# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=rpm
pkgver=4.19.1
pkgrel=0
pkgdesc="Redhat Package Management utilities (RPM)"
url="https://rpm.org/"
arch="all"
license="GPL-2.0-or-later LGPL-2.0-or-later"
_luaver=5.4
depends_dev="
	acl-dev
	binutils-dev
	bzip2-dev
	file-dev
	libarchive-dev
	libcap-dev
	lua$_luaver-dev
	openssl-dev
	popt-dev
	sqlite-dev
	xz-dev
	zlib-dev
	"
makedepends="
	$depends_dev
	cmake
	elfutils-dev
	gawk
	gettext-dev
	graphviz
	libtool
	py3-setuptools
	python3-dev
	readline-dev
	zstd-dev
	"
options="!check" # tests require running a podman/bubblewrap container
subpackages="$pkgname-dev $pkgname-doc $pkgname-lang py3-rpm:py_rpm"
source="https://ftp.osuosl.org/pub/rpm/releases/rpm-${pkgver%*.*}.x/rpm-$pkgver.tar.bz2
	fix-glibc-glob.patch
	link-intl.patch
	"

# secfixes:
#   4.18.0-r0:
#     - CVE-2021-35937
#     - CVE-2021-35938
#     - CVE-2021-35939
#   4.17.1-r0:
#     - CVE-2021-3521
#   4.16.1.3-r0:
#     - CVE-2021-3421
#     - CVE-2021-20271
#     - CVE-2021-20266

prepare() {
	default_prepare

	# Switch to sqlite db by default, including during build-time tests
	sed -i -e "/_db_backend/ s/@DB_BACKEND@/sqlite/g" macros.in
}

build() {
	export CFLAGS="$CFLAGS -flto=auto"

	cmake -B _build \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DENABLE_PYTHON=ON \
		-DENABLE_SQLITE=ON \
		-DENABLE_BDB_RO=ON \
		-DENABLE_NLS=ON \
		-DENABLE_PLUGINS=OFF \
		-DENABLE_TESTSUITE="$(want_check && echo ON || echo OFF)" \
		-DLUA_LIBRARIES="$(pkg-config --variable=libdir lua$_luaver)/liblua.so" \
		-DLUA_INCLUDE_DIR="$(pkg-config --variable=includedir lua$_luaver)" \
		-DWITH_ARCHIVE=ON \
		-DWITH_INTERNAL_OPENPGP=ON \
		-DWITH_OPENSSL=ON \
		-DWITH_AUDIT=OFF \
		-DWITH_SELINUX=OFF
	cmake --build _build
}

package() {
	DESTDIR="$pkgdir" cmake --install _build
}

py_rpm() {
	pkgdesc="Python3 bindings for RPM"

	replaces="py-rpm" # Backwards compatibility
	provides="py-rpm=$pkgver-r$pkgrel" # Backwards compatibility

	amove usr/lib/python3*
}

sha512sums="
12e6c7294a98032418ec9a0510a8183658483fe713d67e6890a9c0da44748371df45a26af6055d08470b85b5dec0cf94795d17c5b3e11db08b20ef07e8e06642  rpm-4.19.1.tar.bz2
432bfbdb681401e54fe437030ff3b4a06a89eb65d1fbff23ddf7e4286f3cc8b647d43b210b0973907216defe614b708ee3d0f365a40cf67351f2a667f231d885  fix-glibc-glob.patch
d55a15bf91ecd3e3809865e291bff4af6e63c15c5de05cb760b05b1dc0ab7458a97a58ec670f05ca95a63e08481eb86bf82d754a019b33801d7af9d585d2ed48  link-intl.patch
"
